package com.njym.payment.alipay;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.demo.trade.service.AlipayTradeService;
import com.alipay.demo.trade.service.impl.AlipayTradeServiceImpl;
import com.njym.entity.SysPaymentConfig;
import com.njym.entity.SysPaymentMode;
import com.njym.service.SysPaymentModeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author zhy
 * 获取对应机构下的支付宝SDK
 * @date 2019-03-22 17:14
 **/
@Component
public class GetAliPayClient {
	@Autowired
	private SysPaymentModeService sysPaymentModeService;

	private Lock lock = new ReentrantLock();

	private static Map<String, AlipayClient> clientMap = new HashMap<>();

	private static Map<String, SysPaymentMode> paymentModeMap = new HashMap<>();

	/**
	 * 通过orgId获取配置
	 */
	private Map<String, AliPayProperties> aliPayPropertiesMap = new HashMap<>();

	/**
	 * 通过appId获取配置
	 */
	private Map<String, AliPayProperties> aliPayPropertiesMap2 = new HashMap<>();

	/**
	 * 通过appId获取service
	 */
	private Map<String, AlipayTradeService> alipayTradeServiceMap = new HashMap<>();

	public AliPayProperties getAliPayPropertiesByOrgId(String orgId) {
		AliPayProperties aliPayProperties = aliPayPropertiesMap.get(orgId);
		if (aliPayProperties != null) {
			return aliPayProperties;
		} else {
			lock.lock();
			AliPayProperties aliPayProperties1;
			try {
				aliPayProperties1 = getProperties(orgId);
				if (aliPayProperties1 == null) {
					return null;
				}
				aliPayPropertiesMap.put(orgId, aliPayProperties1);
				aliPayPropertiesMap2.put(aliPayProperties1.getAppid(), aliPayProperties1);
				aliPayPropertiesMap2.put(aliPayProperties1.getAppId(),aliPayProperties1);
			}
			finally {
				lock.unlock();
			}
			return aliPayProperties1;
		}
	}

	public SysPaymentMode getPaymentModeByOrgId(String orgId) {
		SysPaymentMode sysPaymentMode = paymentModeMap.get(orgId);
		if (sysPaymentMode != null) {
			return sysPaymentMode;
		} else {
			lock.lock();
			SysPaymentMode sysPaymentMode1;
			try {
				sysPaymentMode1 = sysPaymentModeService.selectModeIncludesConfigs(orgId, "alipay");
				if (sysPaymentMode1 == null) {
					return null;
				}
				paymentModeMap.put(orgId, sysPaymentMode1);
			}
			finally {
				lock.unlock();
			}
			return sysPaymentMode1;
		}
	}

	public AlipayTradeService getServiceByAppId(String appId) {
		AlipayTradeService alipayTradeService;
		alipayTradeService = alipayTradeServiceMap.get(appId);
		if(alipayTradeService!=null){
			return alipayTradeService;
		}else {
			lock.lock();
			try {
				AliPayProperties aliPayProperties = aliPayPropertiesMap2.get(appId);
				alipayTradeService =
					new AlipayTradeServiceImpl.ClientBuilder().setGatewayUrl(aliPayProperties.getOpenApiDomain()).setAppid(aliPayProperties.getAppid()).setPrivateKey(aliPayProperties.getPrivateKey())
						.setAlipayPublicKey(aliPayProperties.getAlipayPublicKey()).setSignType(aliPayProperties.getSignType()).build();
				alipayTradeServiceMap.put(appId,alipayTradeService);
			} finally {
				lock.unlock();
			}
			return alipayTradeService;
		}
	}


	public AlipayClient getClientByOrgId(String orgId) {
		AlipayClient aliPayClient = clientMap.get(orgId);
		if (aliPayClient != null) {
			return aliPayClient;
		} else {
			lock.lock();
			AlipayClient alipayClients;
			try {
				AliPayProperties aliPayProperties = getProperties(orgId);
				alipayClients = new DefaultAlipayClient(
					aliPayProperties.getOpenApiDomain(),
					aliPayProperties.getAppid(),
					aliPayProperties.getPrivateKey(),
					"json",
					"UTF-8",
					aliPayProperties.getAlipayPublicKey(),
					aliPayProperties.getSignType());

				clientMap.put(orgId, alipayClients);
			}
			finally {
				lock.unlock();
			}
			return alipayClients;
		}
	}

	private AliPayProperties getProperties(String orgId) {
		SysPaymentMode sysPaymentMode = sysPaymentModeService.selectModeIncludesConfigs(orgId, "alipay");
		if (sysPaymentMode == null) {
			return null;
		}
		List<SysPaymentConfig> sysPaymentConfigList = sysPaymentMode.getSysPaymentConfigs();
		JSONObject jsonObj = new JSONObject();
		for (SysPaymentConfig sysPaymentConfig : sysPaymentConfigList) {
			String key = sysPaymentConfig.getConfigKey();
			String value = sysPaymentConfig.getConfigValue();
			jsonObj.put(key, value);
		}
		AliPayProperties aliPayProperties;
		try {
			aliPayProperties = JSON.toJavaObject(jsonObj, AliPayProperties.class);
		}
		catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return aliPayProperties;
	}

	public Map<String, AliPayProperties> getAliPayPropertiesMap2() {
		return aliPayPropertiesMap2;
	}


}
